package arkham.data;

import arkham.data.Globals.EffectUsage;
import arkham.data.Globals.UsablePhase;
import arkham.data.Investigator.InvestigatorPower;
import arkham.jaxb.encounter.*;
import arkham.util.AHLogger;

public class UsableEffect extends Effect
{
    private int id;
    private UsablePhase phase;
    private EventNode event;

    private UsableEffect(UsablePhase phase, int id)
    {
        this.phase = phase;
        this.id = id;
    }

    /**
     * Gets the id for this usable effect
     * @return the id
     */
    public int getId()
    {
        return id;
    }

    /**
     * Gets the phase when this effect is usable
     * @return the phase
     */
    public UsablePhase getPhase()
    {
        return phase;
    }

    /*
     * (non-Javadoc)
     * @see arkham.data.Effect#getIdentifier()
     */
    @Override
    public String getIdentifier()
    {
        return getSourceString() + "," + id;
    }

    /**
     * @param event the event to set
     */
    public void setEvent(EventNode event)
    {
        this.event = event;
    }

    /**
     * @return the event
     */
    public EventNode getEvent()
    {
        return event;
    }

    /**
     * Creates the data representation of the usable effect from the XML info
     * and performs a stringent validation check to make sure the XML data is
     * valid.
     * <br>Will return <b>null</b> on a failure.
     *
     * @param xmleffect the XML effect info
     * @return the data representation of the usable effect
     */
    public static UsableEffect createModEffectFromXml(UsableEffectType xmleffect)
    {
        UsablePhase phase = UsablePhase.getPhase(xmleffect.getPhase());
        if (phase == null)
        {
            AHLogger.error("Invalid phase on usable effect: " + xmleffect.getPhase());
            return null;
        }
        UsableEffect ueff = new UsableEffect(phase, xmleffect.getId());
        boolean valid = EffectUsage.assignUsage(ueff, xmleffect.getUsage());
        if (!valid)
        {
            AHLogger.error("Invalid usage string on temp effect: " + xmleffect.getUsage());
            return null;
        }
        ueff.getExtraCosts().addAll(xmleffect.getExtraCost());
        if (xmleffect.getEvent() != null)
        {
            ueff.event = xmleffect.getEvent();
        }
        else
        {
            AHLogger.error("No event defined for usable effect.");
            return null;
        }
        return ueff;
    }

    @Override
    public boolean equals(Object o)
    {
        if (!(o instanceof UsableEffect))
        {
            return false;
        }
        UsableEffect ueff = (UsableEffect)o;
        return ueff.id == id && source.equals(ueff.getSource());
    }
}
